/**
 * 新游戏场景
 * @author none
 *
 */
class NewGameSceneView extends V.GameView {
    public static AllowReportChildren: Array<any> = ["PublicCardsPanel", "GameEffectPanel"];
    private dichChipsPanel: ChipsPanel;
    private menuButton: how.Button;//菜单按钮
    private backButton: how.Button;//返回按钮
    public settingBtn: how.Button;//设置按钮
    public cardTypeImg: eui.Image;//所有牌型大小的图片
    public cardTypeButton: how.Button;//牌型按钮
    private backMenuGroup: eui.Group;//菜单面板分组
    public gameRecord: how.Button;//游戏记录
    public publicCardsPanel: PublicCardsPanel;//公共拍面板
    public gameEffectPanel: GameEffectPanel;//特效面板
    // public gameData: NewGameSceneData;
    public mangzhuLabel: how.Label;//显示盲注信息的label
    public heguanIcon: eui.Image;//荷官图标
    public heguanAnimator: how.Animator;//荷官动画组件
    public zhayanInterval: number = null;//荷官眨眼定时器
    public heguanSay: eui.Group;//荷官提示语
    public heguanSayLabel: how.Label;//荷官提示语文本
    public qianzhuBtn: how.Button;//前注按钮
    public screenshotBtn: how.Button;//截屏按钮
    public pipeiTip: eui.Group;//匹配窗口
    public messageLabel: how.Label;//匹配提示语
    public intervalNumber: number = null;//匹配提示动画循环
    public cancelButton: how.Button;//取消匹配按钮
    public resumeGame: how.Button;//继续游戏按钮
    public gameIdGroup: eui.Group;//游戏流水号显示组
    public gameIdLabel: how.Label;//游戏流水号
    public heguanTimeout: number;//荷官说话延迟任务
    public table_light1: eui.Image;//桌子呼吸灯
    public table_light2: eui.Image;//桌子呼吸灯
    public dailog: how.Dialog;
    public alert: how.Alert;
    /** 最大操作时间 */
    private _PLAY_CD: number = 25;
    /** 当前操作用户的位置 */
    public cSit:number;
    /** 当前操作用户 */
    public cPv:NewPlayerView; 
    /** 操作界面 */
    public operatePanel:OperatePanelView;

    public constructor() {
        super();
        this.MAX_PLAYER = 9;
        this.skinName = "GameSceneSkin";
        this.Res = DataConfig.ResConfig[DataConfig.KIND_ID_DZPK];
    }
    
    protected childrenCreated(): void {
        this.initPlayer();
        this.backButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBackButton, this);
        //this.bgArea.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouchGroup, this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouchGroup, this);
        this.cardTypeButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCardTypeButton, this);
        this.settingBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSettingBtn, this);
        this.menuButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onMenuButton, this);
        this.qianzhuBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onQianzhuBtn, this);
        this.screenshotBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onScreenshotBtn, this);
        this.cancelButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCancelButton, this);
        this.resumeGame.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onResumeGame, this);
        this.heguanAnimator.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onHeguanIcon, this);
        this.gameRecord.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGameRecord, this);
        this.gameEffectPanel.visible = true;
        this.gameEffectPanel.heguanIcon = this.heguanIcon;
        //显示当前房间盲注信息
        if (!!AppData.getInstance().currentRoom) {
            this.mangzhuLabel.text = how.StringUtils.format(LanguageConfig.mangzhu,
                base.Utils.formatCurrency(AppData.getInstance().currentRoom.cellScore),
                base.Utils.formatCurrency(AppData.getInstance().currentRoom.cellScore * 2));//盲注;
            if (AppData.getInstance().currentRoom.preScore > 0) {
                this.mangzhuLabel.text = this.mangzhuLabel.text + how.StringUtils.format(LanguageConfig.pairPlusTip,
                    base.Utils.formatCurrency(AppData.getInstance().currentRoom.preScore)
                )
            }
        }
        this.screenshotBtn.visible = false;
        this.gameIdGroup.visible = false;
        this.backMenuGroup.visible = false;
        this.resumeGame.visible = false;
        this.qianzhuBtn.visible = AppData.getInstance().currentRoom.preScore > 0;
        this.heguanSay.visible = AppData.getInstance().currentRoom.preScore > 0;
        this["dichifly"] = this.dichChipsPanel.scoreIcon;
        if (AppData.getInstance().currentRoom.preScore > 0) {
            this.onHeguanSay(how.StringUtils.format(LanguageConfig.qianzhuTipLabelText,
                base.Utils.formatCurrency(AppData.getInstance().currentRoom.preScore)));
        }
        this.hideLight();
        this.zhayanInterval = egret.setInterval(() => {
            this.heguanAnimator.play("zhayan", 1);
        }, this, 15000);
        this.initGameView();
        how.SoundManager.stopMusic();
        how.SoundManager.playMusic("dzpk_gamebg_mp3", false);
    }
    /** 收到网络消息 */
    public onNetData(s:number, d:any):void{
        switch(s){
            case 100:{ //游戏开始
                this.onGameStart(d);
                break;
            }case 101:{ //玩家操作完成
                let sit = this.c2p(d.wAddScoreUser);
                this.playerList[sit].hideOpt({lAddScoreCount:d.lAddScoreCount,bAddGiveUp:d.bAddGiveUp,lTurnLessScore:this.operatePanel.lTurnLessScore,lTurnMaxScore:this.operatePanel.lTurnMaxScore});
                let type = 0;
                if(d.bAddGiveUp == 0){
                    if(d.lAddScoreCount == this.operatePanel.lTurnMaxScore) type = 5;
                    else if(d.lAddScoreCount > this.operatePanel.lTurnLessScore) type = 4;
                    else if(d.lAddScoreCount > 0) type = 1;
                    this.operatePanel.setPreOperateUI();
                }else{
                    this.gameEffectPanel.showGiveupEffect(<NewPlayerView>this.playerList[sit]);
                    if(D.App.userData.chairID == d.wAddScoreUser){
                        this.operatePanel.showGameEndShowCard();
                        // this.showResumeGame();
                    }
                }
                this.gameEffectPanel.moveChipsTo(this.playerList[sit]["head"], this.playerList[sit]["playerfly1"], d.lAddScoreCount, type);
                this.showOpView(d);
                this.updataDiChi(d.lAddScoreCount);
                break;
            }case 103:{ //收到发牌
                this.publicCardsPanel.cards = d.cbCenterCardData;
                this.onGetCards(d);
                // this.updataDiChi();
                this.operatePanel.updateGenzhuLabel();
                for(let i=0;i<this.MAX_PLAYER;i++){
                    let pv:NewPlayerView = <NewPlayerView>this.playerList[i];
                    if(pv.addScore > 0){
                        egret.setTimeout(() => {
                            this.gameEffectPanel.moveChipsTo(pv["playerfly1"], this["dichifly"], pv["chipPanel"].data);
                            pv["chipPanel"].visible = false;
                            how.SoundManager.playEffect("effect_chipflychizi_mp3");
                            pv.addScore = 0;
                        }, this, 800);
                    }
                }
                let pv:NewPlayerView = <NewPlayerView>this.playerList[0];
                let cindex = pv.tipMycardType(d.cbCenterCardData);
                this.publicCardsPanel.showCenterCardTip(cindex);
                this.showOpView(d);
                break;
            }case 104:{ //游戏结束
                this.onGameEnd(d);
                break;
            }
        }
    }
    public onDestroy(): void {
        egret.Tween.removeAllTweens();
        for (var i = 1; i <= 3; i++) {
            egret.Tween.removeTweens(this["table_light" + i]);
        }
        egret.clearInterval(this.zhayanInterval);
    }
    // public get resourceList(): Array<string> {
    //     return ["dzpk_cards", "dzpk_gameScene"];
    // }
    //显示继续游戏按钮
    public showResumeGame(): void {
        this.resumeGame.visible = true;
        this.resumeGame.labelDisplay.text = "";
    }
    //隐藏继续游戏按钮
    public heidResumeGame(): void {
        this.resumeGame.visible = false;
    }
    //按下取消匹配按钮
    public onCancelButton(): void {
        this.backMenuGroup.visible = false;
        // this.report("onCancelButton");
        WebSocketInstance.getHallSocket().send(CMDConfig.SEND_STANDUP, {});
        this.gameEffectPanel.cleanEffectCards();
        this.publicCardsPanel.resetData();
    }
    //按下继续游戏按钮
    public onResumeGame(): void {
        this.backMenuGroup.visible = false;
        // // this.report("onResumeGame");
    }
    //按下截屏按钮
    public onScreenshotBtn(): void {
        this.backMenuGroup.visible = false;
        // var renderTexture: egret.RenderTexture = new egret.RenderTexture();
        // renderTexture.drawToTexture(this, new egret.Rectangle(0, 0, how.Application.app.stage.stageWidth, how.Application.app.stage.$stageHeight));
        // renderTexture.saveToFile("image/png", base.Utils.formatDate(new Date(), "yyyy-MM-dd hh:mm:ss") + ".png", new egret.Rectangle(0, 0, how.Application.app.stage.stageWidth, how.Application.app.stage.$stageHeight));
        var canvas: HTMLCanvasElement = <HTMLCanvasElement>document.getElementsByTagName("canvas")[0];
        var dataImg = canvas.toDataURL('image/png');
        var w = window.open('about:blank', 'image from canvas');
        w.document.write("<img src='" + dataImg + "' alt='from canvas'/>");
    }
    //初始化游戏场
    public initGameView(): void {
        //显示当前房间盲注信息
        console.log("AppData.getInstance().currentRoom :", AppData.getInstance().currentRoom);
        if (!!AppData.getInstance().currentRoom)
        {
            this.mangzhuLabel.text = how.StringUtils.format(LanguageConfig.mangzhu,
                base.Utils.formatCurrency(AppData.getInstance().currentRoom.cellScore),
                base.Utils.formatCurrency(AppData.getInstance().currentRoom.cellScore * 2));//盲注;
            if (AppData.getInstance().currentRoom.preScore > 0)
            {
                this.mangzhuLabel.text = this.mangzhuLabel.text + how.StringUtils.format(LanguageConfig.pairPlusTip,
                    base.Utils.formatCurrency(AppData.getInstance().currentRoom.preScore)
                )
            }
        }
        this.qianzhuBtn.visible = AppData.getInstance().currentRoom.preScore > 0;
        this.hideLight();
    }
    //显示匹配界面
    public showPiPeiUI(): void {
        this.gameIdGroup.visible = false;
        this.pipeiTip.visible = true;
        var tipIndex = 0;
        this.hideLight();
        if (this.intervalNumber) {
            egret.clearInterval(this.intervalNumber);
        }
        this.intervalNumber = egret.setInterval(() => {
            if (tipIndex == 4) {
                tipIndex = 0;
            }
            var text = LanguageConfig.pipeiTip;
            for (var i = 0; i < tipIndex; i++) {
                text += ".";
            }
            this.messageLabel.text = text;
            tipIndex++;
        }, this, 300);
    }
    //隐藏匹配界面
    public hidePiPeiUI(): void {
        this.pipeiTip.visible = false;
        egret.clearInterval(this.intervalNumber);
        this.intervalNumber = null;
    }
    //按下前注按钮
    public onQianzhuBtn(): void {
        this.backMenuGroup.visible = false;
        how.module.ModuleManager.getInstance().initModule(QianZhuTipWindowModule, QianZhuTipWindowView);
    }
    //按下设置按钮
    private onSettingBtn(): void {
        this.backMenuGroup.visible = false;
        how.module.ModuleManager.getInstance().initModule(SettingWindowModule, SettingWindowView, SettingWindowData);
    }
    //按下牌型按钮
    public onCardTypeButton(): void {
        this.cardTypeImg.scaleY = (this["gameBg"].height - 85) / this.cardTypeImg.height;
        this.cardTypeImg.scaleX = (this["gameBg"].height - 85) / this.cardTypeImg.height;
        this.backMenuGroup.visible = false;
        // // this.report("showcardTypeImg");
    }
    //点击荷官
    public onHeguanIcon(): void {
        this.onHeguanSay(LanguageConfig["heguanSay" + base.Utils.getRandom(0, 2)]);
    }
    //荷官说话
    public onHeguanSay(text: string): void {
        this.heguanAnimator.play("smail", 1);
        this.heguanSayLabel.text = text;
        this.heguanSay.visible = true;
        if (this.heguanTimeout) {
            egret.clearTimeout(this.heguanTimeout);
            this.heguanTimeout = null;
        }
        this.heguanTimeout = egret.setTimeout(() => {
            this.heguanSay.visible = false;
        }, this, 3000);
    }
    //点击背景区域
    public onTouchGroup(e:egret.TouchEvent): void {
        this.backMenuGroup.visible = false;
        this.operatePanel.hideQuickOperatePanel();
        this.hideCardTypeImg();
        // this.report("onTouchBg");
    }
    //隐藏牌型面板
    public hideCardTypeImg(): void {
        this.cardTypeImg.visible = false;
    }
    //按下返回按钮
    public onBackButton(): void {
        this.backMenuGroup.visible = false;
        // this.report("onBackButton");
        if (D.App.userData.userStatus == UserStatus.PLAY && this.playerList[0].isPlay()) {
            this.alert = how.Alert.show(LanguageConfig.onBackButtonPlayerisGaming, () => {
                this.alert = null;
            });
        } else {
            this.dailog = how.Dialog.show(LanguageConfig.onBackButtonTip, function(){
                WebSocketInstance.getHallSocket().send(CMDConfig.SEND_LEAVE_GAME, {});
                WebSocketInstance.getHallSocket().send(CMDConfig.SEND_LEAVE_ROOM, {});
            }, function () {
                this.dailog = null;
            }, this);
        }
    }
    //按下游戏记录按钮
    public onGameRecord(): void {
        this.backMenuGroup.visible = false;
        how.module.ModuleManager.getInstance().initModule(GameRecordModule, GameRecordView);
        // this.report("onGameRecord");
    }
    //按下菜单按钮
    public onMenuButton(e:egret.TouchEvent): void {
        this.backMenuGroup.visible = !this.backMenuGroup.visible;
        this.cardTypeImg.visible = false;
        e.stopPropagation();
    }
    //显示牌型图片
    public showcardTypeImg(): void {
        this.backMenuGroup.visible = false;
        this.cardTypeImg.visible = true;
    }
    //显示游戏id
    public showGameId(id: string): void {
        var kindIDText = AppData.getInstance().currentGame.id.toString();
        for (var i = 0; i < 4 - AppData.getInstance().currentGame.id.toString().toString().length; i++) {
            kindIDText = "0" + kindIDText;
        }
        if (id) {
            this.gameIdGroup.visible = true;
            var gameId = id.toString();
            var chairID = AppData.getInstance().userData.chairID + 1;
            var tableID = AppData.getInstance().userData.tableID + 1;
            this.gameIdLabel.text = kindIDText + AppData.getInstance().currentRoom.id.toString() + (parseInt(gameId) * chairID)
                + "-" + parseInt(chairID.toString() + tableID.toString()) * 5;
        }
    }
    //当游戏开始
    public onGameStart(data: any): void {
        this.cellScore = data.lCellScore;
        this.gameEffectPanel.cleanEffectCards();
        this.gameEffectPanel.cleanEffectTips();
        this.gameEffectPanel.cleanGameOverEffects();
        if(D.App.userData.chairID == this.playerList[0].chairId) this.operatePanel.myPv = <NewPlayerView>this.playerList[0];
        else this.operatePanel.myPv = null;
        this.publicCardsPanel.cardNum = 0;
        this.publicCardsPanel.cards = [];
        this.resetPublicCardsData();
        this.dichChipsPanel.data = 0;
        this.updataDiChi(this.cellScore * 3);
        this.alpha = 1;
        // this.showGameId(data.gameId);
        this.hideLight();
        this.operatePanel.visible = false;
        this.heguanAnimator.play("kiss", 1);
        data.wSmallChipInUser = this.getPre(data.wMaxChipInUser); //计算小盲玩家
        for(let i=0;i<this.MAX_PLAYER;i++){
            this.playerList[i].onGameStart(data);
        }
        //播放发牌特效
        var playerCount = 0;
        for (var j = 0; j < 2; j++) {
            var z = 0;
            for (var i = 0; i < RoomData.MAX_PLAYER; i++) {
                let pv:NewPlayerView = <NewPlayerView>this.playerList[i];
                if(pv.isPlay()){
                    this.gameEffectPanel.playCardEffectNew(pv, this.heguanIcon, j, z, playerCount);
                    if (j == 0) {
                        playerCount++;
                    }
                    z++;
                }
            }
        }
        egret.setTimeout(function(){
            console.log("opuser", data.wCurrentUser);
            this.showOpView(data);
        }, this, playerCount*300);
    }
    private gameEndPlayers:NewPlayerView[];
    /** 当游戏结束 */
    public onGameEnd(data:any): void {
        this.hideLight();
        if (!AppData.getInstance().isGameHide) {
            //判断玩家是否全都是第一名（也就是公共牌最大）
            var flag: boolean = false;
            for (var i = 0; i < data.UserRank.length; i++) {
                if (data.UserRank[i] > 1) {
                    flag = true;
                    break;
                }
            }
            this.gameEndPlayers = [];
            for (var i = 0; i < this.MAX_PLAYER; i++) {
                let pv:NewPlayerView = <NewPlayerView>this.playerList[i];
                pv.endRank = data.UserRank[pv.chairId];
                pv.endScore = data.lGameScore[pv.chairId];
                pv.endTax = data.lGameTax[pv.chairId];
                pv.endType = data.cbLastCardKind[pv.chairId];
                pv.endCards = data.cbLastCenterCardData[pv.chairId];
                if(pv.addScore > 0){
                    this.gameEffectPanel.moveChipsTo(pv["playerfly1"], this["dichifly"], pv["chipPanel"].data);
                    pv["chipPanel"].visible = false;
                    how.SoundManager.playEffect("effect_chipflychizi_mp3");
                }
                if(pv.totalScore > 0) pv.totalScore += data.lGameScore[pv.chairId];
                //console.log("end", pv.chairId, pv.totalScore, pv.giveUp, pv.endRank);
                if(pv.giveUp == false && pv.endRank != 0){
                    if(pv.endRank == 1 && pv.endCards[0] != 0){
                        how.SoundManager.playEffect("effect_bipai_mp3");
                    }
                    pv.handCard = data.cbCardData[pv.chairId];
                    this.gameEffectPanel.showPlayerCard(pv);
                    if(pv.totalScore > 0) this.gameEndPlayers.push(pv);
                }
            }
            if (flag) {//如果不是底牌最大，则更具牌型进行排序
                this.gameEndPlayers.sort(function (a, b) {
                    return a.endRank > b.endRank ? 1 : -1;
                });
            }
            egret.Tween.get(this).wait(800).call(this.gameEndEffectComplete, this);
        }
    }
    /** 游戏结束动画播放完毕 */
    public gameEndEffectComplete(): void {
        var playerEffect: NewPlayerView = this.gameEndPlayers.shift();
        console.log("gameEndEffectComplete", playerEffect);
        if (playerEffect) {
            let cardsData = this.publicCardsPanel.cards
            this.gameEffectPanel.cleanEffectCards();
            playerEffect.hidecardTypeTip();
            this.publicCardsPanel.showCardData(cardsData);
            this.publicCardsPanel.hideCenterCardTip();
            egret.Tween.get(this).wait(1000).call(this.showGameEndCardType, this, [playerEffect]);
        } else {
            if(D.App.gameView){
                for (var i = 0; i < this.MAX_PLAYER; i++) {
                    let pv:NewPlayerView = <NewPlayerView>this.playerList[i];
                    if(pv.totalScore > 0) this.gameEffectPanel.showGameEndScoreResultEffect(pv);
                }
            }
        }
    }
    /** 亮起最终牌型和玩家手牌 */
    public showGameEndCardType(pv: NewPlayerView): void {
        if (pv.endCards[0] != 0) {
            let cardsData = this.publicCardsPanel.cards;
            for (var j = 0; j < cardsData.length; j++) {
                var isBright = false;
                if (cardsData[j] && cardsData[j] != 0) {
                    isBright = base.Utils.indexOf(pv.endCards, cardsData[j]) != -1;
                }
                if (isBright) {//上移
                    this.gameEffectPanel.onGameOverCardRight(this.publicCardsPanel, j, cardsData[j]);
                    this.publicCardsPanel["card" + j].visible = false;
                } else {
                    this.publicCardsPanel.showCardRect(j);
                }
            }
            var cardsIndexs: number[] = [];
            for (var i = 0; i < pv.endCards.length; i++) {
                var index = pv.handCard.indexOf(pv.endCards[i]);
                if (index != -1) {
                    cardsIndexs.push(index);
                }
            }
            pv.showMyCardTip(cardsIndexs, true);
            if(pv.endRank >= 7 && pv.isMe()){
                this.gameEffectPanel.playCardTypeDaYuHuLuEffect(pv.endType);
            }
        }
        if(pv.endScore > 0){
            pv.victoryUI(pv.endType);
            if(pv.isMe()){
                var sound = pv.endType > 6 ? "effect_win_mp3" + pv.endType + "_mp3" : "effect_win_mp3";
                how.SoundManager.playEffect(sound);
            }
        }
        var time = pv.endRank == 1 ? 1300 : 200;
        //筹码飞向玩家
        egret.Tween.get(this).wait(time).call(() => {
            // this.gameEffectPanel.moveChipsTo(this.gui["dichifly"], playerModule.gui["head"], playerModule.data.lGameScore);
            this.gameEffectPanel.moveChipsTo(this["dichifly"], pv["head"], pv.totalScore);
            this.updataDiChi(-(pv.totalScore + pv.endTax));
            if (this.gameEndPlayers.length == 0) {
                // NewGameSceneData.getInstance().dichiValue = 0;
            }
            how.SoundManager.playEffect("effect_chipflypeople_mp3");
        });
        egret.Tween.get(this).wait(2000).call(this.gameEndEffectComplete, this);//一秒钟后播放下一个赢牌玩家的动画
    }
    /** 同步桌子 */
    public onSyncData(d:any):void{
        if(D.App.userData.chairID == this.playerList[0].chairId) this.operatePanel.myPv = <NewPlayerView>this.playerList[0];
        else this.operatePanel.myPv = null;
        if(D.App.table.gameStatus == 100){
            this.showPlayer(65535); //这里暂时这样用一下显示所有玩家
            this.operatePanel.updateGenzhuLabel();
            if (!RES.isGroupLoaded("dzpk_sounds") || !RES.isGroupLoaded("dzpk_font")) {
                egret.Tween.get(this).wait(2000).call(() => {
                    if (!RES.isGroupLoaded("dzpk_sounds")) {
                        RES.loadGroup("dzpk_sounds");
                    }
                    if (!RES.isGroupLoaded("dzpk_font")) {
                        RES.loadGroup("dzpk_font");
                    }
                }, this);
            }
            how.EventManager.getInstance(null).dispatchEvent(LoaddingGlobalModule.response.hideLoad, { ".isCommunicate": true, communicateData: null });
            d.wSmallChipInUser = this.getPre(d.wMaxChipInUser); //计算小盲玩家
            this.dichChipsPanel.data = 0;
            for(let i=0;i<this.MAX_PLAYER;i++){
                this.dichChipsPanel.data += d.lTotalScore[i];
                this.playerList[i].onSyncData(d);
                if(d.lTotalScore[i] > 0 && d.cbPlayStatus[i] == 0){
                    this.gameEffectPanel.showGiveupEffect(<NewPlayerView>this.playerList[this.c2p(i)]);
                }
            }
            this.updataDiChi(0);
            this.publicCardsPanel.showCardData(d.cbCenterCardData);
            if(d.cbHandCardData[0] > 0 && d.cbPlayStatus[D.App.userData.chairID] == 0){ //自己弃牌了
                this.operatePanel.showGameEndShowCard();//如果为弃牌则显示结束时亮牌按钮
                let pv:NewPlayerView = <NewPlayerView>this.playerList[0];
                let cindex = pv.tipMycardType(d.cbCenterCardData);
                this.publicCardsPanel.showCenterCardTip(cindex);
            }
            this.showOpView(d);
        }
    }

    /** 显示操作视图 */
    public showOpView(d:any){
        if(d.lTurnMaxScore){
            this.operatePanel.lTurnLessScore = d.lTurnLessScore;
            this.operatePanel.lTurnMaxScore = d.lTurnMaxScore;
            this.operatePanel.lAddLessScore = d.lAddLessScore;
        }
        if(d.wCurrentUser == D.App.userData.chairID){ //当前用户是自己显示操作面板
            this.operatePanel.setOperateUI();
        }else{
            this.operatePanel.visible = false;
        }
        if(d.wCurrentUser != 65535){
            this.cSit = this.c2p(d.wCurrentUser);
            this.cPv = <NewPlayerView>this.playerList[this.cSit];
            this.cPv.showOpt();
            this.showLight(this.cSit);

            let operatorTime = 0;
            this.cPv._shakeIndex = -1;
            this.cPv.progressTimer.value = operatorTime != 0 ? (operatorTime + 5) / this._PLAY_CD * 100 : 0;
            if (this.cPv.progressTimer.value < 100) {
                egret.Tween.get(this.cPv.progressTimer, { onChange: this.onRingJudge, onChangeObj: this }).
                    to({ value: 100 }, (this._PLAY_CD - operatorTime) * 1000).//倒计时10秒
                    call(this.onTimeOver, this);//倒计时结束;
            }
            else {
                this.cPv.progressTimer.value = 100;
            }
            this.cPv.progressTimer.startBlinkTween();
        }
    }
    /** 倒计时进行中 */
    private onRingJudge(): void {
        if (this.cPv.progressTimer.value >= (this._PLAY_CD - 3) / this._PLAY_CD * 100) {
            if (this.cPv._shakeIndex != 0) {
                this.cPv._shakeIndex = 0;
                // // this.report("onTimeRing", this._shakeIndex);
            }
        }
    }
    /** 倒计时结束 */
    private onTimeOver(): void {
        egret.Tween.removeTweens(this.cPv.progressTimer);
        if(this.cSit == 0 && D.App.userData.userStatus == UserStatus.PLAY){
            this.operatePanel.autoLead();
        }
    }

    //当断线重连
    // // public onReConnected(data: NewGameSceneData): void {
    // //     this.updataDiChi();
    // //     // this.showPlayer(65535);
    // //     this.publicCardsPanel.showCardData(data.publicCards);
    // //     this.publicCardsPanel.cardData = data.publicCards;
    // // }
    //更新底池
    public updataDiChi(v:number): void {
        this.dichChipsPanel.data += v;
        this.operatePanel.dichiValue = this.operatePanel.quickOperatePanel.dichiValue = this.dichChipsPanel.data;
        // this.dichChipsPanel.data = NewGameSceneData.getInstance().dichiValue;
        this.dichChipsPanel.visible = this.dichChipsPanel.data > 7;
    }
    //收到发牌，也是一轮结束     
    public onGetCards(data: any) {
        if (!AppData.getInstance().isGameHide) {
            var index: number = 0;
            // for (var i: number = this.publicCardsPanel.cardData.length; i < data.publicCards.length; i++) {
            //     this.gameEffectPanel.playPublicCardEffect(this.publicCardsPanel, index * 300, i, this.heguanIcon);
            //     index++;
            // }
            for (let i=this.publicCardsPanel.cardNum; this.publicCardsPanel.cards[i] > 0 && i < 5; i++) {
                this.gameEffectPanel.playPublicCardEffect(this.publicCardsPanel, index * 300, i, this.heguanIcon);
                index++;
            }
        }
        // this.publicCardsPanel.cardData = data.publicCards;
        // this.updataDiChi();
        if (AppData.getInstance().isGameHide) {
            // this.publicCardsPanel.showCardData(this.publicCardsPanel.cardData);
            this.publicCardsPanel.showCardData();
            // // this.report("showCardTypeTip");
        }
    }
    //重置公共牌
    public resetPublicCardsData(): void {
        this.publicCardsPanel.resetData();
    }
    //提示最大牌型中的自己的手牌
    public showMyCardTip(cardsIndexs: number[], isGameEnd: boolean): void {
        this.hidecardTypeTip();
        for (var i = 0; i < cardsIndexs.length; i++) {
            var card: eui.Image = this["cardTypeTip" + cardsIndexs[i]];
            card.visible = true;
            if (!isGameEnd) {
                card.source = "cardTypeTip1_png";
            } else {
                card.source = "cardTypeTip2_png";
            }
        }
        if (isGameEnd) {
            this["cardRect" + 0].visible = cardsIndexs.indexOf(0) == -1;
            this["cardRect" + 1].visible = cardsIndexs.indexOf(1) == -1;
        }
    }
    //隐藏提示牌型框
    public hidecardTypeTip(): void {
        for (var i = 0; i < 2; i++) {
            this["cardTypeTip" + i].visible = false;
            this["cardRect" + i].visible = false;
        }
    }
    //显示某一个聚光灯
    public showLight(index: number): void {
        index = index == 9 ? 0 : index;
        for (var i = 0; i < 9; i++) {
            this["light" + i].visible = index == i;
        }
    }
    //隐藏所有聚光灯
    public hideLight(): void {
        for (var i = 0; i < 9; i++) {
            this["light" + i].visible = false;
        }
    }
    //显示呼吸灯动画
    public showBreatheLight(type: number): void {
        for (var i = 1; i <= 3; i++) {
            switch (type) {
                case 1:
                    if (i == 3) {
                        egret.Tween.get(this["table_light" + i], { loop: true }).to({ alpha: 0.2 }, 2000).to({ alpha: 1 }, 2000);
                    } else {
                        egret.Tween.get(this["table_light" + i], { loop: true }).to({ alpha: 0 }, 2000).to({ alpha: 1 }, 2000);
                    }
                    break;
            }
        }

    }
}